var casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
});

casper.start();

casper.then(function step1() {
    this.echo('this is step one');
});

casper.then(function step2() {
    this.echo('this is step two');
});

casper.then(function() {
    var titleNode = document.querySelector('h1');
    this.echo('Title is: ' + titleNode.textContent);
    titleNode.textContent = 'New title';
    this.echo('Title is now: ' + titleNode.textContent);
});

// this will
casper.then(function() {
    var titleText = this.evaluate(function() {
        return document.querySelector('h1').textContent;
    });
    this.echo('Title is: ' + titleText);
    this.evaluate(function() {
        document.querySelector('h1').textContent = 'New title';
    });
    this.echo('Title is now: ' + this.evaluate(function() {
        return document.querySelector('h1').textContent;
    }));
});

// this will
casper.then(function() {
    this.echo('Title is: ' + this.fetchText('h1'));
    this.evaluate(function() {
        document.querySelector('h1').textContent = 'New title';
    });
    this.echo('Element HTML is now: ' + this.getElementInfo('h1').html);
});

casper.page.onResourceRequested = function(requestData, networkRequest) {
   var match = requestData.url.match(/wordfamily.js/g);
   if (match != null) {
      console.log('Request (#' + requestData.id + '): ' + JSON.stringify(requestData));

      // overrides wordfamily.js to local newWordFamily.js
      networkRequest.changeUrl('newWordFamily.js');
   }
};
casper.thenOpen('https://www.google.com/?gws_rd=cr,ssl', function initialAppearance() {
  casper.waitForText('Text in deep part of page or modal');
});
casper.thenOpen('https://www.google.com/?gws_rd=cr,ssl', function step3() {
    this.echo('this is step 3 (google.com is loaded)');
});

require('utils').dump(casper.steps.map(function(step) {
    return step.toString();
}));
casper.run();